package co.yixiang.business.like

import co.yixiang.annotation.AnonymousAccess
import co.yixiang.api.ApiResult
import co.yixiang.entity.Like
import co.yixiang.entity.LikeRepository
import co.yixiang.entity.Post
import co.yixiang.util.PageModel
import co.yixiang.util.getApiResult
import co.yixiang.util.toListVo
import co.yixiang.util.toPage
import io.swagger.annotations.Api
import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

interface LikeService {

    /**
     * 保存我的点赞
     */
    fun saveLike(like: Like): ApiResult<*>

    /**
     * 获取我的点赞列表
     */
    fun getMyLike(pageModel: PageModel): ApiResult<*>

    /**
     * 获取我的点赞数量
     */
    fun getMyLikeCount(userId: Long): Int
}

data class LikeListVo(
    var id: String? = "",
    var likeId: Long? = null,
    var likePic: String? = "",
)

@Service
class LikeServiceImp(@Autowired private var likeRepository: LikeRepository,
                     @Autowired val database: Database): LikeService {


    override fun saveLike(like: Like): ApiResult<*> {

        return likeRepository.getLikeByLikeIdAndUserId(like.likeId, like.userId).blockFirst()?.run {
            likeRepository.deleteById(id!!).block().getApiResult(200, "取消点赞成功")
        } ?: likeRepository.save(like).block().getApiResult(200, "点赞成功")
    }


    override fun getMyLike(pageModel: PageModel): ApiResult<*> {

         return likeRepository.getLikesByUserIdOrderByCreateTimeDesc(pageModel.userId).toIterable()
            .toMutableList().takeIf { it.isNotEmpty() }?.run {
                toListVo { it ->
                    when (it.type) {
                        0 -> {
                           LikeListVo().apply {
                               id = it.id
                               likeId = it.likeId
                               likePic =
                                   database.from(Post).select(Post.pics).where { Post.id.eq(it.likeId!!) }.map { row -> row[Post.pics] }
                                       .takeIf { it.isNotEmpty() }?.get(0)?.split(",")?.get(0)
                           }
                        }
                        else -> {
                        }
                    }
                }.toPage(pageModel.page!!, pageModel.pageSize!!).getApiResult(200, "获取我点赞的帖子成功")
             } ?: getApiResult(201, "暂无数据")

}

    override fun getMyLikeCount(userId: Long): Int {

        return likeRepository.getLikesByUserIdOrderByCreateTimeDesc(userId).toIterable().count()
    }

    @Api("我的点赞接口")
@RequestMapping("/like")
@RestController
class LikeController {

    @Autowired
    private lateinit var likeService: LikeService

    @PostMapping("/saveLike")
    @AnonymousAccess
    fun saveLike(like: Like): ApiResult<*> = likeService.saveLike(like)

    @PostMapping("/getMyLike")
    @AnonymousAccess
    fun getMyLike(pageModel: PageModel): ApiResult<*> = likeService.getMyLike(pageModel)
}

}
